package com.mxpio.erp.plm.service.impl;

import com.mxpio.erp.common.enums.CommonEnums;
import com.mxpio.erp.common.enums.OrderStatusEnums;
import com.mxpio.erp.common.plm.entity.*;
import com.mxpio.erp.common.plm.service.ProjectDeliverableDraftService;
import com.mxpio.erp.common.plm.service.ProjectInfoChangeService;
import com.mxpio.erp.common.plm.service.ProjectInfoDraftService;
import com.mxpio.erp.common.plm.service.ProjectInfoRecordDeliverableDraftService;
import com.mxpio.erp.common.plm.vo.ProjectInfoDetailVO;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 项目信息变更
 */
@Service
public class ProjectInfoChangeServiceImpl extends BaseServiceImpl<ProjectInfoChange> implements ProjectInfoChangeService {

    @Autowired
    private ProjectInfoDraftService projectInfoDraftService;
    @Autowired
    private ProjectInfoRecordDeliverableDraftService projectInfoRecordDeliverableDraftService;
    @Autowired
    private ProjectDeliverableDraftService projectDeliverableDraftService;

    @Override
    @Transactional
    public Result<?> listall(String bizNo) {
        ProjectInfoChange byId = this.getById(ProjectInfoChange.class, bizNo);
        ProjectInfoDraft draft = projectInfoDraftService.getById(ProjectInfoDraft.class, byId.getProCode());
        ArrayList<Object> objects = new ArrayList<>();
        objects.add(byId);
        objects.add(draft);
        return Result.OK(objects);
    }

    @Override
    @Transactional
    public Result<ProjectInfoDraft> getOne(String bizNo) {
        ProjectInfoDraft one = JpaUtil.linq(ProjectInfoDraft.class).equal("bizNo", bizNo).findOne();
        return Result.OK(one);


//        List<ProjectInfoChange> list = this.list(ProjectInfoChange.class, Criteria.create().addCriterion("proCode", Operator.EQ, proCode));
//        for (ProjectInfoChange projectInfoChange : list) {
//            if (projectInfoChange.getBpmnStatus().equals(OrderStatusEnums.BpmnStatus.CREATE.getStatusCode())
//                    || projectInfoChange.getBpmnStatus().equals(OrderStatusEnums.BpmnStatus.AUDITING.getStatusCode())) ;
//            List<ProjectInfoDraft> drafts = projectInfoDraftService.list(ProjectInfoDraft.class, Criteria.create().addCriterion("proCode", Operator.EQ, proCode));
//            for (ProjectInfoDraft projectInfoDraft : drafts) {
//                if (projectInfoDraft.getIsAuditEnd().equals(CommonEnums.YesNo.NO.getCode())
//                        || projectInfoDraft.getIsAuditEnd() == null) {
//                    projectInfoDraft.setReason(projectInfoChange.getReason());
//                    projectInfoDraft.setChangeType(projectInfoChange.getChangeType());
//                    projectInfoDraftService.update(projectInfoDraft);
//                    return Result.OK(projectInfoDraft);
//                }
//            }
//        }
//        return Result.error("查询不到数据");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<?> getDetail(String bizNo) {
        ProjectInfoDetailVO projectInfoDetailVO = new ProjectInfoDetailVO();
        ProjectInfoDraft infoDraft = JpaUtil.linq(ProjectInfoDraft.class).equal("bizNo", bizNo).findOne();
        List<ProjectDeliverableDraft> deliverableDrafts = projectDeliverableDraftService.list(ProjectDeliverableDraft.class, Criteria.create().addCriterion("bizNo", Operator.EQ, bizNo));
        projectInfoDetailVO.setProjectInfoDraft(infoDraft);
        projectInfoDetailVO.setProjectDeliverableDrafts(deliverableDrafts);
        return Result.OK(projectInfoDetailVO);
    }
}
